package org.example;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.bootstrap.builders.ReferenceBuilder;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.config.spring.reference.ReferenceBeanBuilder;
import org.apache.dubbo.rpc.RpcContext;
import org.example.service.DemoService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@EnableDubbo
@SpringBootApplication
public class ConsumerApp implements CommandLineRunner {

    @DubboReference(methods = {
            @Method(name = "sayHello", timeout = 5000)
    })
    private DemoService demoService;

    public static void main( String[] args ) {
        SpringApplication.run(ConsumerApp.class, args);
//        testMain();

//        testMain01();
    }

    public static void testMain01() {
        RegistryConfig hzRegistry = new RegistryConfig();
        hzRegistry.setId("hz");
        hzRegistry.setAddress("nacos://192.168.70.36:8848");
        DemoService demoService01 = ReferenceBuilder.<DemoService>newBuilder()
                .interfaceClass(DemoService.class)
                .addRegistry(hzRegistry)
                .url("dubbo://127.0.0.1:50051")
                .build()
                .get();
        System.out.println(demoService01.sayHello("tang!"));
    }

    public static void testMain() {

        DemoService demo = (DemoService)ReferenceBuilder.newBuilder()
                .interfaceClass(DemoService.class)
                .url("dubbo://127.0.0.1:50051")
                .build()
                .get();

        System.out.println(demo.sayHello("tang!"));

    }

    @Override
    public void run(String... args) throws Exception {
//        demoService.sayHelloServerStream("tang!", new StreamObserver<String>() {
//            @Override
//            public void onNext(String data) {
//                System.out.println("ConsumerApp onNext : " + data);
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//
//            }
//
//            @Override
//            public void onCompleted() {
//
//            }
//        });

//        StreamObserver<String> streamObserver = demoService.sayHelloStream(new StreamObserver<String>() {
//            @Override
//            public void onNext(String data) {
//                System.out.println("ConsumerApp : " + data);
//            }
//
//            @Override
//            public void onError(Throwable throwable) {
//
//            }
//
//            @Override
//            public void onCompleted() {
//                System.out.println("ConsumerApp onCompleted");
//            }
//        });
//
//        streamObserver.onNext("test!");
//        streamObserver.onCompleted();
        RpcContext.getClientAttachment().setAttachment("name", "tang");
        System.out.println("result " + demoService.sayHello("tang!"));
        String attachment = RpcContext.getServerContext().getAttachment("name");
        System.out.println("consumer attachment " + attachment);

    }
}
